package com.administration;

import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.ParkingspacesModel;
import com.webservice.DaoSingleObject;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Sawior
 */
@ManagedBean
@RequestScoped
public class Charts implements Serializable {

    private static final String PERSONAL = "Personal";
    private static final String VAN = "Van";
    private static final String MOTORCYCLE = "Motorcycle";
    private static final String TRAILER = "Trailer";
    
    private static final String FREE = "Free";
    private static final String OCCUPIED = "Occupied";
    private static final String DISABLED = "Disabled";
    private static final String ALL = "All";
    private static final String SPECIAL = "Special";
    private Map<String, Integer> statistic = new HashMap<String, Integer>();
    private PieChartModel pieModel;
  private final DAO dao = getSingleDao();
    DaoSingleObject dso;

    
    @PostConstruct
    public void createPieModel() {
        statistic = new HashMap<String, Integer>();
        statistic.put(PERSONAL, 0);
        statistic.put(VAN, 0);
        statistic.put(MOTORCYCLE, 0);
        statistic.put(TRAILER, 0);
        statistic.put(FREE, 0);
        statistic.put(OCCUPIED, 0);
        statistic.put(DISABLED, 0);
        statistic.put(ALL, 0);
        statistic.put(SPECIAL, 0);
        List<ParkingspacesModel> spaces = dao.getAllParkingSpaces();
        for (ParkingspacesModel space : spaces) {
            countCarsTypes(space);
            countSpacesStates(space);
        }
    }

    public PieChartModel getChartByCarType() {
        pieModel = new PieChartModel();
        pieModel.set(PERSONAL, statistic.get(PERSONAL));
        pieModel.set(VAN, statistic.get(VAN));
        pieModel.set(MOTORCYCLE, statistic.get(MOTORCYCLE));
        pieModel.set(TRAILER, statistic.get(TRAILER));
        return pieModel;
    }

    public PieChartModel getChartByStatus() {
        pieModel = new PieChartModel();
        pieModel.set(FREE, statistic.get(FREE));
        pieModel.set(OCCUPIED, statistic.get(OCCUPIED));
        pieModel.set(DISABLED, statistic.get(DISABLED));
        return pieModel;
    }
  public DAO getSingleDao() {
        if (this.dso == null) {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            this.dso = (DaoSingleObject) app.evaluateExpressionGet(context, "#{daoSingleObject}", DaoSingleObject.class);
        }
        return this.dso.getDao();
    }
    private void countCarsTypes(ParkingspacesModel space) {
        Integer currentValue = statistic.get(space.getType());
        currentValue++;
        statistic.put(space.getType(), currentValue);
    }

    private void countSpacesStates(ParkingspacesModel space) {
        Integer currentValue = statistic.get(space.getStatus());
        currentValue++;
        statistic.put(space.getStatus(), currentValue);
    }
}
